<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src/ux/Ux.Value.js - Zero UI Doc</title>
    <meta name="description" content="Zero UI前端文档工具">
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="stylesheet" href="../assets/css/custom.css">
    <link rel="stylesheet" href="../assets/vendor/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="../assets/vendor/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../assets/css/theme.css">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,600,700" rel="stylesheet" type="text/css">
    <link rel="shortcut icon" type="image/png" href="../assets/img/favicon.png">
    
</head>
<body>
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <div class="navbar-header">
          <a href="../" class="navbar-brand">
            <img src="../assets/img/mdeditor-logo.png" alt="">
            <span>API Docs</span>
          </a>
        </div>

        <div class="collapse navbar-collapse" id="nav">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="https://github.com/silentbalanceyh/vertx-ui.git" class="fa fa-github github"></a></li>
          </ul>
        </div>
      </div>
    </nav>
    <div id="main-wrapper" class="row">
        <div id="content-wrapper">
            <ol class="panel-group" id="sidebar" role="tablist" aria-multiselectable="true">
                <li class="panel panel-default">
                    <div class="panel-heading" role="tab" id="sidebar-search-heading">
                        <h4 class="panel-title">
                      <a role="button" data-toggle="collapse" href="#sidebar-search" aria-expanded="true" aria-controls="collapseOne">
                        Search
                      </a>
                    </h4>
                    </div>
                    <div id="sidebar-search" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="sidebar-search-heading">
                        <div class="panel-body">
                            <div id="api-tabview-filter">
                                <input type="search" id="api-filter" placeholder="Search...">
                            </div>
                        </div>
                    </div>
                </li>
                    <li class="panel panel-default">
                        <div class="panel-heading" role="tab" id="sidebar-version-heading">
                            <h4 class="panel-title">
                                <a role="button" href="https://github.com/silentbalanceyh/vertx-ui.git/commits/0" target="_blank">
                                  Tag: 0.1.0
                                </a>
                            </h4>
                        </div>
                    </li>
                <li class="panel panel-default">
                    <div class="panel-heading" role="tab" id="sidebar-modules-heading">
                        <h4 class="panel-title">
                    <a role="button" data-toggle="collapse" href="#sidebar-modules" aria-expanded="true" aria-controls="collapseOne">
                      Modules
                    </a>
                  </h4>
                    </div>
                    <div id="sidebar-modules" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="sidebar-modules-heading">
                        <div class="panel-body">
                                <ol>
                                        <li>
                                            <a href="../modules/Ux.html">Ux</a>
                                        </li>
                                </ol>
                        </div>
                    </div>
                </li>
            
                <li class="panel panel-default">
                    <div class="panel-heading" role="tab" id="sidebar-classes-heading">
                        <h4 class="panel-title">
                      <a role="button" data-toggle="collapse" href="#sidebar-classes" aria-expanded="true" aria-controls="collapseOne">
                        Classes
                      </a>
                    </h4>
                    </div>
                    <div id="sidebar-classes" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="sidebar-classes-heading">
                        <div class="panel-body">
                            <ol>
                                    <li>
                                        <a href="../classes/Action.html">Action</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Ajax.html">Ajax</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Attribute.html">Attribute</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Column.html">Column</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Debug.html">Debug</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Depend.html">Depend</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Dialog.html">Dialog</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Encrypt.html">Encrypt</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Env.html">Env</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Expr.html">Expr</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Field.html">Field</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Format.html">Format</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Global.html">Global</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Hoc.html">Hoc</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Html.html">Html</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Icon.html">Icon</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Jsx.html">Jsx</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Log.html">Log</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Normalize.html">Normalize</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Op.html">Op</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Option.html">Option</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Param.html">Param</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Prop.html">Prop</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Random.html">Random</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Redux.html">Redux</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Sorter.html">Sorter</a>
                                    </li>
                                    <li>
                                        <a href="../classes/State.html">State</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Store.html">Store</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Type.html">Type</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Validator.html">Validator</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Value.html">Value</a>
                                    </li>
                            </ol>
                        </div>
                    </div>
                </li>
            </ol>
            <div class="content-container">
                <div class="apidocs">
                    <div id="docs-main">
                        <div class="content">
<div class="page-header">
    <h1><i class="fa fa-file-code-o" aria-hidden="true"></i>  File</h1>
</div>

<div class="file">
    <pre class="prettyprint linenums">
import moment from &#x27;moment&#x27;;
import Dg from &#x27;./Ux.Debug&#x27;;

/**
 * 不重复追加值到&#x60;item&#x60;对象中（包含则不设置）
 * @method valueAppend
 * @param item 被设置的对象引用
 * @param field 设置的字段名
 * @param value 设置的字段值
 */
const valueAppend = (item = {}, field = &quot;&quot;, value) =&gt; {
    if (!item.hasOwnProperty(field)) {
        item[field] = value;
    }
};
/**
 * 直接转换数据成Moment对象，时间处理
 * @method convertTime
 * @param value 输入数据
 * @return {*}
 */
const convertTime = (value) =&gt; {
    if (value) {
        if (!moment.isMoment(value)) {
            value = moment(value);
            if (!moment.isMoment(value)) {
                console.error(&quot;[V] Convert &#x27;value&#x27; to invalid &#x27;Moment&#x27; object.&quot;);
            }
        }
        return value;
    } else {
        console.error(&quot;[V] The &#x27;value&#x27; is undefined, could not be converted.&quot;);
    }
};
/**
 * 连续乘法专用乘法计算
 * @method mathMultiplication
 * @param seed 第一操作数
 * @param ops 其他操作数
 * @return {*}
 */
const mathMultiplication = (seed, ...ops) =&gt; {
    seed = parseFloat(seed);
    let result = isNaN(seed) ? 0.00 : seed;
    ops.forEach(op =&gt; {
        op = parseFloat(op);
        if (!isNaN(op)) {
            result *= op;
        }
    });
    return result;
};
/**
 * 专用除法运算
 * @method mathDivision
 * @param dividend 被除数
 * @param divisor 除数
 * @return {number}
 */
const mathDivision = (dividend, divisor) =&gt; {
    dividend = parseFloat(dividend);
    divisor = parseFloat(divisor);
    if (!isNaN(dividend) &amp;&amp; !isNaN(divisor) &amp;&amp; 0 !== divisor) {
        return dividend / divisor;
    } else {
        console.info(&quot;[Math] dividend / divisor = &quot;, dividend, divisor)
    }
};
/**
 * 根据from和to计算中间的duration差值
 * * years - y
 * * monthds -M
 * * weeks -w
 * * days - d
 * * hours - h
 * * minutes - m
 * * seconds - s
 * * milliseconds - ms
 * @method valueDuration
 * @param from 开始时间
 * @param to 结束时间
 * @param mode 计算模式
 */
const valueDuration = (from, to, mode = &#x27;day&#x27;) =&gt; {
    if (from &amp;&amp; to) {
        from = convertTime(from);
        to = convertTime(to);
        return moment(to).diff(from, mode);
    } else {
        console.error(&quot;[V] Either &#x27;from&#x27; or &#x27;to&#x27; must not be undefined.&quot;);
    }
};
/**
 * 根据开始时间计算结束时间
 * @method valueEndTime
 * @param from 开始时间
 * @param duration 时差
 * @param mode 计算模式
 * @return {moment.Moment}
 */
const valueEndTime = (from, duration, mode = &#x27;day&#x27;) =&gt; {
    if (from &amp;&amp; duration) {
        from = convertTime(from);
        Dg.ensurePositive(duration);
        return moment(from).add(duration, mode);
    } else {
        console.error(&quot;[V] Either &#x27;from&#x27; or &#x27;duration&#x27; must not be undefined.&quot;)
    }
};
/**
 * 根据结束时间计算开始时间
 * @method valueStartTime
 * @param to 结束时间
 * @param duration 时差
 * @param mode 计算模式
 * @return {moment.Moment}
 */
const valueStartTime = (to, duration, mode = &#x27;day&#x27;) =&gt; {
    if (to &amp;&amp; duration) {
        to = convertTime(to);
        Dg.ensurePositive(duration);
        return moment(to).subtract(duration, mode);
    } else {
        console.error(&quot;[V] Either &#x27;to&#x27; or &#x27;duration&#x27; must not be undefined.&quot;)
    }
};
/**
 * 针对JavaScript中的对象进行过滤
 * @method valueFilter
 * @param data 被过滤的数据对象
 * @param keys 保留的字段名集合
 */
const valueFilter = (data = {}, keys = []) =&gt; {
    const result = {};
    keys.forEach(key =&gt; {
        if (data.hasOwnProperty(key)) {
            result[key] = data[key];
        }
    });
    return result;
};
/**
 * @class Value
 * @description 数值计算器
 */
export default {
    valueAppend,
    valueDuration,
    valueEndTime,
    valueStartTime,
    valueFilter,
    // 数学运算
    mathMultiplication,
    mathDivision,
    // 转换处理
    convertTime
}

    </pre>
</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../assets/vendor/prettify/prettify-min.js"></script>
    <script>prettyPrint();</script>
    <script src="../assets/vendor/jquery/jquery.min.js"></script>
    <script src="../assets/vendor/jquery-ui/jquery-ui.min.js"></script>
    <script src="../assets/vendor/bootstrap/js/bootstrap.js"></script>
    <script src="../assets/vendor/github-slugger/slugger.js"></script>
    <script src="../assets/js/yuidoc-bootstrap.js"></script>
</body>
</html>
